package cn.huq.day01

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object WordCount {

  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf().setAppName("wordcount")

    val sc = new SparkContext(conf)

    val lines: RDD[String] = sc.textFile(args(0))

    val sorted: RDD[(String, Int)] = lines.flatMap(_.split(" "))
      .map((_, 1))
      .reduceByKey(_ + _)
      .sortBy(_._2, false)

    sorted.saveAsTextFile(args(1))

    sc.stop()
  }

}
